package com.liohi.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.liohi.Param.R;
import com.liohi.entity.User;
import com.liohi.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@CrossOrigin
@RestController
@Api(tags = "用户管理")
@RequestMapping("user-manage")
public class UserManageController {

    @Autowired
    private UserService userService;


    @ApiOperation("添加用户")
    @PostMapping("add")
    public R addUser(@RequestBody User user){
        boolean saved = userService.save(user);
        if(saved)return R.ok("添加成功");
        else return R.error("添加失败");
    }

    @ApiOperation("条件查询用户")
    @PostMapping("list/page")
    public R listUserByPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, String username, String name) {
        IPage<User> page = userService.listByPage(pageNum, pageSize, username, name);
        return R.ok("data",page);
    }

    @ApiOperation("删除用户")
    @DeleteMapping ("remove/{id}")
    public R remove(@PathVariable Integer id){
        if(id == null)return R.error("参数错误");
        boolean flag = userService.removeById(id);
        return flag?R.ok("删除成功"):R.error("删除用户失败");
    }

    @ApiOperation("根据Id查询用户")
    @GetMapping("getById/{id}")
    public  R getById(@PathVariable Integer id){
        if(id==null)return R.error("参数错误");
        User user = userService.getById(id);
        return R.ok("data",user);
    }

    @ApiOperation("根据id修改用户")
    @PutMapping("updateById/{id}")
    public R updateById(@RequestBody User user){
        return userService.updateUser(user);
    }
}
